package com.hejia.bulbul.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.SplashScreen;
import com.hejia.alauda.service.ConfigureService;
import com.hejia.alauda.utils.HttpResponse;

/**
 * 启动画面控制器
 * 已使用新启动画面管理界面，老版以后需清除
 * 
 * @author hyr
 *
 */
@Deprecated
@Controller
@RequestMapping("splashscreens")
public class SplashScreenController {

	private static final Log Logger = LogFactory.getLog(SplashScreenController.class);
	
	@Resource
	private ConfigureService parameterService;
	
	@Resource
	private ConfigureService splashScreenService;
	
	@RequestMapping(value = "setdisplaytime", method = RequestMethod.POST)
	public String setDisplayTime(String os, Date startTime, Date endTime){
		
		try{
			this.splashScreenService.setDisplayTime(os, startTime, endTime);
		}catch(Exception e){
			Logger.error("设置显示时间失败", e);
		}
		
		return "redirect:/splashscreens";
	}
	
	@RequestMapping(value = "set", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse setSplashScreen(SplashScreen splashScreen){
		
		HttpResponse response = new HttpResponse();
		try{
			Logger.info(splashScreen.getUrl());
			this.splashScreenService.setSplashScreen(splashScreen);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("设置启动画面成功");
			
		}catch(Exception e){
			Logger.error("设置启动画面失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("设置启动画面失败 " + e.getMessage());
		}
		return response;
	}
	
	@RequestMapping
	public String list(ModelMap model){
		
		Date iOSStartTime = null;
		Date iOSEndTime = null;
		Date androidStartTime = null;
		Date androidEndTime = null;
		List<SplashScreen> splashScreens = this.splashScreenService.findAllSplashScreens();
		for(SplashScreen splashScreen : splashScreens){
			if(splashScreen.getOs().equals("iOS")){
				iOSStartTime = splashScreen.getStartTime();
				iOSEndTime = splashScreen.getEndTime();
			}else if(splashScreen.getOs().equals("Android")){
				androidStartTime = splashScreen.getStartTime();
				androidEndTime = splashScreen.getEndTime();
			}
			model.put(splashScreen.getCode(), splashScreen);
		}
		
		model.put("iOSStartTime", iOSStartTime);
		model.put("iOSEndTime", iOSEndTime);
		model.put("androidStartTime", androidStartTime);
		model.put("androidEndTime", androidEndTime);
		
		return "splashscreen/list";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    dateFormat.setLenient(false);
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}
